﻿using MediatR;
using SystemWFH.API.Application.Command.EquipmentCommand;
using SystemWFH.Infrstructer.Impl;
using SystemWFH.Infrstructer.Interface;

namespace SystemWFH.API.Application.CommandHandlers.EquipmentCommandHandler
{
    public class EquipmentDeleteCommandHandler : IRequestHandler<EquipmentDeleteCommand, int>
    {
        /// <summary>
        /// 服务
        /// </summary>
        private readonly IEquipmentRepository equipmentRepository;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="equipmentRepository">服务</param>
        public EquipmentDeleteCommandHandler(IEquipmentRepository equipmentRepository)
        {
            this.equipmentRepository = equipmentRepository;
        }
        public async Task<int> Handle(EquipmentDeleteCommand request, CancellationToken cancellationToken)
        {
            //if (request.equipmentId.Length == 0)
            //{
            //    return 0;
            //}
            //string[] arrer = request.equipmentId.Split(',');
            //var str = await equipmentRepository.GetAsync(x => arrer.Contains((x.equipmentId).ToString()));
            //str.ForEach(x =>
            //{
            //    x.DelectName = request.DelectName;
            //    x.DelectTime=DateTime.Now.ToUniversalTime();
            //    x.IsDelete = true;
            //});
            //var list = await equipmentRepository.GetDelete(str);
            //return list;
            var list = (await equipmentRepository.GetAll()).Where(x => request.equipmentId.Contains(x.equipmentId)).ToList();
            foreach (var item in list)
            {
                item.IsDelete = true;
                await equipmentRepository.Update(item);
            }
            return 1;
        }

    }
}
